package com.shenfa.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.Template;
import com.shenfa.bean.TemplateArea;
import com.shenfa.service.TemplateAreaService;
import com.shenfa.service.TemplateService;

import com.shenfa.util.ResultObject;

public class TemplateAreaAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8842258357161448199L;

	@Autowired
	private TemplateAreaService tempareaservice;

	@Autowired
	private TemplateService templateService;
	
	private TemplateArea temparea;

	private Template template;

	private List<TemplateArea> tempareas;
	
	private static Logger log=Logger.getLogger(TemplateAreaAction.class);

	/** 查询个性区域化信息 **/
	public String list() {
		log.debug(temparea.toString());
		List<TemplateArea> list = tempareaservice.QueryTemplateArea(temparea);
		result = ResultObject.build("0", list);

		return SUCCESS;
	}

	/**
	 * 添加个性区域化信息
	 * 
	 * @return
	 */
	public String add() {
		if(null!=tempareas){
			for (int i = 0; i < tempareas.size(); i++) {
				log.debug(tempareas.get(i).toString());
			}
		}
		log.debug(template.toString());
		
		int type = 0;
		if (null != tempareas) {
			tempareaservice.del(tempareas.get(0));
			for (TemplateArea tempare : tempareas) {
				type = tempareaservice.add(tempare);
				if (type == 0)
					break;
			}
		} else {
			temparea = new TemplateArea();
			temparea.setTempno(template.getId());
			type = tempareaservice.del(temparea);
			type = 1;
		}
		if (type != 0)
			type = templateService.upd(template);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "操作成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	public TemplateArea getTemparea() {
		return temparea;
	}

	public void setTemparea(TemplateArea temparea) {
		this.temparea = temparea;
	}

	public List<TemplateArea> getTempareas() {
		return tempareas;
	}

	public void setTempareas(List<TemplateArea> tempareas) {
		this.tempareas = tempareas;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

}
